import 'package:dio/dio.dart';
import 'package:net_store/Entities/Category.dart';
import 'package:net_store/Entities/HotStore.dart';
import 'package:net_store/Entities/Product.dart';
import 'package:net_store/Entities/Recommandation.dart';
import 'package:net_store/Entities/ServerResponse.dart';
import 'package:net_store/Entities/Store.dart';

class HttpDataSource {
  String _apiUrl;
  Dio _dio;
  HttpDataSource(this._apiUrl){
    _dio = Dio(Options(
      baseUrl: _apiUrl
    ));
  }

  Future<List<Product>> getAllProducts() async {
    var response = await _dio.get("/api/product");
    var data = ServerResponse.fromJson(response.data, (json) => (json as List<dynamic>).map((json) => Product.fromJson(json)).toList());
    data.result.forEach((product) => product.images = product.images.map((url) => "$_apiUrl/$url").toList());
    return data.result;
  }

  Future<List<Recommandation>> getAllRecommandations() async {
    var response = await _dio.get("/api/recommendproduct");
    var data = ServerResponse.fromJson(response.data, (json) => (json as List<dynamic>).map((recommandation) => Recommandation.fromJson(recommandation)).toList());
    return data.result;
  }

  Future<List<HotStore>> getAllHotStories() async {
    var response = await _dio.get("/api/hotbazar");
    var data = ServerResponse.fromJson(response.data, (json) => (json as List<dynamic>).map((json) => HotStore.fromJson(json)).toList());
    data.result.forEach((store) => store.icon = "$_apiUrl/${store.icon}");
    return data.result;
  }

  Future<List<Store>> getAllStories() async {
    var response = await _dio.get("/api/bazar");
    var data = ServerResponse.fromJson(response.data, (json) => (json as List<dynamic>).map((json) => Store.fromJson(json)).toList());
    data.result.forEach((store) => store.icon = "$_apiUrl/${store.icon}");
    return data.result;
  }

  Future<List<Category>> getCategoriesByStore(String storeId) async {
    var result = await _dio.get("/api/discription/getbybazar?bazar=$storeId");
    var data = ServerResponse.fromJson(result.data, (json) => (json as List<dynamic>).map((json) => Category.fromJson(json)).toList());
    data.result.forEach((store) => store.icon = "$_apiUrl/${store.icon}");
    return data.result;
  }

  Future<List<Product>> getProductsByCategory(String categoryId) async {
    var response = await _dio.get("/api/product/getbydiscription?discription=$categoryId");
    var data = ServerResponse.fromJson(response.data, (json) => (json as List<dynamic>).map((json) => Product.fromJson(json)).toList());
    data.result.forEach((product) => product.images = product.images.map((url) => "$_apiUrl/$url").toList());
    return data.result;
  }

  Future<List<Product>> getProductsOfStore(String storeId) async {
    var response = await _dio.get("/api/product/getbybazar?bazar=$storeId");
    var data = ServerResponse.fromJson(response.data, (json) => (json as List<dynamic>).map((json) => Product.fromJson(json)).toList());
    data.result.forEach((product) => product.images = product.images.map((url) => "$_apiUrl/$url").toList());
    return data.result;
  }
}

